package cn.tedu.egweibo.controller;

import cn.tedu.egweibo.mapper.BlogMapper;
import cn.tedu.egweibo.pojo.dto.BlogDTO;
import cn.tedu.egweibo.pojo.entity.Blog;
import cn.tedu.egweibo.pojo.entity.User;
import cn.tedu.egweibo.result.JsonResult;
import cn.tedu.egweibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Api(tags = "博客模块")
@RestController
@RequestMapping("/weibo")
@Validated
public class BlogController {
    @Autowired
    private BlogMapper blogMapper;
    @PostMapping("/intser")
    @ApiOperation("发表微博")
    public JsonResult addBlog(@RequestBody BlogDTO blogDTO, @ApiIgnore HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Blog blog = new Blog();
        blog.setContent(blogDTO.getContent());
        blog.setCreated(new Date());
        blog.setUserId(blog.getId());
        blogMapper.insertBlog(blog);
        return JsonResult.success();

    }
}
